package com.zjxf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.CmsSchoolTutor;
import com.zjxf.base.bean.po.UmsStoreSeller;
import com.zjxf.base.bean.ro.RoCmsSchoolTutor;
import com.zjxf.base.bean.vo.VoCmsSchoolTutor;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.repository.CmsSchoolTutorRepository;
import com.zjxf.service.CmsSchoolTutorService;
import com.zjxf.utils.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 长征大学导师
 *
 * @author ZhangWei
 * @date 2021/4/7/007 10:31
 */
@Service
@AllArgsConstructor
@Transactional(rollbackOn = RuntimeException.class)
public class CmsSchoolTutorServiceImpl implements CmsSchoolTutorService {

    private final CmsSchoolTutorRepository cmsSchoolTutorRepository;

    public Optional<CmsSchoolTutor> findById(Long id) {
        return cmsSchoolTutorRepository.findByIdAndDeleteState(id, SysConst.DeleteState.UN_DELETE_STATE.getCode());
    }

    @Override
    public void addOrUpdate(VoCmsSchoolTutor.addOrUpdate vo, UmsStoreSeller umsStoreSeller) {
        Optional<CmsSchoolTutor> optionalCmsLmSchoolTutor = findById(vo.getId());
        CmsSchoolTutor cmsSchoolTutor;
        if (optionalCmsLmSchoolTutor.isPresent()) {
            cmsSchoolTutor = optionalCmsLmSchoolTutor.get();
            cmsSchoolTutor = Conversion.voTurnPo(vo, cmsSchoolTutor);
            cmsSchoolTutor.setDetailText(HtmlUtils.clearTag(vo.getDetailHtml()));
        } else {
            cmsSchoolTutor = Conversion.voTurnPo(vo, new CmsSchoolTutor());
            cmsSchoolTutor.setStoreId(umsStoreSeller.getStoreId());
            cmsSchoolTutor.setDetailText(HtmlUtils.clearTag(vo.getDetailHtml()));
        }
        cmsSchoolTutorRepository.save(cmsSchoolTutor);
    }

    @Override
    public void delete(Long id) throws OperationException {
        CmsSchoolTutor cmsSchoolTutor = findById(id).orElseThrow(() -> new OperationException(SysConst.DATA_NOT_FOND));
        cmsSchoolTutor.setDeleteState(SysConst.DeleteState.DELETE_STATE.getCode());
        cmsSchoolTutorRepository.save(cmsSchoolTutor);
    }

    @Override
    public CmsSchoolTutor getEntity(Long id) throws OperationException {
        return findById(id).orElseThrow(() -> new OperationException(SysConst.DATA_NOT_FOND));

    }

    @Override
    public RoCmsSchoolTutor.Details getEntityRo(CmsSchoolTutor cmsSchoolTutor) {
        return Conversion.poTurnRo(cmsSchoolTutor, new RoCmsSchoolTutor.Details());
    }

    @Override
    public Page<CmsSchoolTutor> page(VoCmsSchoolTutor.filter filter,UmsStoreSeller umsStoreSeller) {
        List<SearchFilter> searchFilters = new ArrayList<>();
        searchFilters.add(new SearchFilter("storeId", SearchFilter.Operator.EQ, umsStoreSeller.getStoreId()));
        if (StringUtils.isNotBlank(filter.getKeywords())) {
            searchFilters.add(new SearchFilter("name", SearchFilter.Operator.LIKE, filter.getKeywords()));
        }
        Specification<CmsSchoolTutor> specification = DynamicSpecifications.bySearchFilterByBasic(searchFilters, filter.getStartTime(), filter.getEndTime());
        return cmsSchoolTutorRepository.findAll(specification, PageUtils.buildPageRequest(filter.getPageNum(), filter.getPageSize()));
    }

    @Override
    public JSONObject pageRo(Page<CmsSchoolTutor> cmsLmSchoolTutorPage) {
        List<CmsSchoolTutor> pageList = cmsLmSchoolTutorPage.getContent();
        List<RoCmsSchoolTutor.Page> roList = pageList.stream().map(item -> {
            RoCmsSchoolTutor.Page page = Conversion.poTurnRo(item, new RoCmsSchoolTutor.Page());
            return page;
        }).collect(Collectors.toList());
        return PageUtils.page(cmsLmSchoolTutorPage, roList);
    }
}
